# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.shortcuts import render, redirect
from django.http import JsonResponse, HttpResponseRedirect, HttpResponse
from .models import User
from task.oa_spider import Spider

# Create your views here.
def login(request):
    print 'login:', request.session.session_key
    if request.method == 'POST':
        if not request.POST:
            request.POST = request.GET
        hasuser = User.objects.filter(username=request.POST['username'],
                                      password=request.POST['password']).count()
        if hasuser:
            request.session['logined'] = 'yes'
            request.session['username'] = request.POST['username']
            print '登录成功'
            return JsonResponse({'status': 1, 'msg': '登录成功'})
        else:
            print '帐号或者密码错误'
            return JsonResponse({'status': 0, 'msg': '帐号或者密码错误'})
    elif request.session.has_key('logined') and request.session['logined'] == 'yes':
        return HttpResponseRedirect('/oa/')
    else:
        request.session['ct'] = 'ct'
        return render(request, 'users/login.html')

def loginout(req):
    del req.session['logined']
    del req.session['username']
    return HttpResponseRedirect('/oa/')

def register (req):
    print 'register:', req.session.session_key
    if req.method == 'POST':
        if not req.POST:
            req.POST = req.GET
        hasuser = User.objects.filter(username=req.POST['username']).count()
        if hasuser:
            return JsonResponse({'status': 0, 'msg': '用户名已经注册'})
        User.objects.create(username=req.POST['username'],
                            password=req.POST['password'])
        print '用户注册成功'
        return JsonResponse({'status': 1, 'msg': '用户注册成功'})
    elif req.session.has_key('logined') and req.session['logined'] == 'yes':
        return HttpResponseRedirect('/oa/')
    else:
        return render(req, 'users/register.html')

def index(req):
    print '/:', req.session.session_key
    if req.session.has_key('logined') and req.session['logined'] == 'yes':
        username = req.session['username']
        pwd = User.objects.get(username=username).password
        spider = Spider(username=username, password=pwd)
        data = spider.get_all_data()
        return render(req, 'users/index.html', {'user': username,
                                                'data': data})
    else:
        return HttpResponseRedirect('/oa/login/')

def get_data(req):
    if req.session.has_key('logined') and req.session['logined'] == 'yes':
        username = req.session['username']
        pwd = User.objects.get(username=username).password
        resdata = {'status': 1, 'msg': '', 'data': {}}
        try:
            spider = Spider(username=username, password=pwd)
            if req.GET.has_key('title'):
                title = req.GET['title']
                resdata['data'] = spider.get_all_data(title)
            else:
                resdata['data'] = spider.get_all_data()
        except Exception, e:
            resdata['msg'] = '该用户非目标网站会员'
        if not resdata['data']:
            resdata['status'] = 0
        return JsonResponse(resdata)
    else:
        return HttpResponseRedirect('/oa/login/')

def get_detail(req):
    if req.session.has_key('logined') and req.session['logined'] == 'yes':
        username = req.session['username']
        pwd = User.objects.get(username=username).password
        resdata = {'status': 1, 'msg': '', 'data': {}}
        try:
            spider = Spider(username=username, password=pwd)
            if req.GET.has_key('funcid') and req.GET.has_key('id') and req.GET.has_key('type'):
                resdata['data'] = spider.get_detail(req.GET['funcid'], req.GET['id'], req.GET['type'])
            else:
                resdata['msg'] = '传入参数错误'
                resdata['status'] = 0
        except Exception, e:
            resdata['msg'] = '该用户非目标网站会员'
        if not resdata['data']:
            resdata['status'] = 0
        return JsonResponse(resdata)
    else:
        return HttpResponseRedirect('/oa/login/')
